package com.jukte.recipe;

import java.util.ArrayList;
import java.util.List;
import com.ewhat.recipes.R;
import com.jukte.recipe.adapters.RecipeListAdapter;
import com.jukte.recipe.db.DataBaseManager;
import com.jukte.recipe.models.Recipe;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SearchActivity extends BaseActivity implements OnClickListener{

	
	private ImageButton imb_search;
	private EditText searchEditText;
	private ListView SearchResultList;
	private List<Recipe> results;
	private RecipeListAdapter adapter;
	private ProgressDialog m_ProgressDialog;
	private String searchQueryString;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		init();
		setUpList();
	}
	
	private void setUpList() {
		// TODO Auto-generated method stub
		adapter=new RecipeListAdapter(SearchActivity.this,0, results);
		SearchResultList.setAdapter(adapter);
		SearchResultList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
				Intent intent=new Intent(SearchActivity.this, RecipeDetailsActivity.class);
				intent.putExtra("RECIPE_ID", results.get(position).getId());
				intent.putExtra("RECIPE_NAME", results.get(position).getName());
				startActivity(intent);
				
			}
		});
	}
	
	@Override
	public void init() {
		imb_search=(ImageButton)findViewById(R.id.imb_search);
		imb_search.setOnClickListener(this);
		searchEditText=(EditText)findViewById(R.id.editText_search);
		SearchResultList=(ListView)findViewById(R.id.listView_search_list);
		results=new ArrayList<Recipe>();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==imb_search)
		{
			searchQueryString=searchEditText.getText().toString().trim();
			if(TextUtils.isEmpty(searchQueryString))
			{
				searchEditText.setError("Enter Recipe Name!!");
			}
			else
			{
				doSearch();
			}
		}
	}
	private void doSearch() {
		// TODO Auto-generated method stub
		Thread thread = new Thread(null, dbFetch, "MagentoBackground");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(this, "",
				"Searching...", true);
		m_ProgressDialog.setCancelable(true);
	}

	

	Runnable dbFetch=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			getRecipeListFromDb();
			runOnUiThread(returnRecipeData);
		}
	};
	
	private void getRecipeListFromDb() {
		// TODO Auto-generated method stub
		results.clear();
		results.addAll(DataBaseManager.getInstance().getSearchResult(searchQueryString));
		DataBaseManager.getInstance().closeDb();
		//Log.e("aise", sites.size()+"");
	}
	Runnable returnRecipeData=new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			m_ProgressDialog.dismiss();
			adapter.notifyDataSetChanged();
			if(results.size()==0)
			{
				Toast.makeText(SearchActivity.this, "No Recipe found. Please try with another name.", Toast.LENGTH_LONG).show();
			}
			
		}
	};

}
